ii=figure;
oo=figure;

colors=[0 0 1;0 0 1;1 0 0;1 0 0;0 0 0;0 0 0;0 0 0;0 0 0;0 0 0;0 0 0;];
% colors2=[1 0 0;1 0 1;0 0 1;0.5 1 0;0 .7 0.5;0.5 0 0;0.4 0 0;0.3 0 0;0.2 0 0;0.1 0 0;];
% % colors2(:,1)= linspace(0,0.7,10);

show_ind_act_raster=1;
show_avg_act_vis_speed=1;
if show_avg_act_vis_speed
    jj=figure;hold on
end
for site=6%size(proj_meta,2)
no_time_points = size(proj_meta(site).rd,2);

act=[];
type_sessions={'f','p','d'};
mean_act=[];
sem_act=[];
for tp=1:no_time_points
    act=[];
    for lyr=1:size(proj_meta(site).rd,1)
        act=[act; proj_meta(site).rd(lyr,tp).act];
    end
    temp=proj_meta(site).rd(lyr,tp).nbr_frames;
    temp=cumsum(temp);
    temp=[1 temp+1];
    velM = proj_meta(site).rd(lyr,tp).velM_smoothed;
    velP = proj_meta(site).rd(lyr,tp).velP_smoothed;
    if show_ind_act_raster
        tt=act;
%         tt(end+1:end+6,:)=repmat(velM>0.005,6,1)*4;
%         tt(end+1:end+6,:)=repmat((velP>0.005),6,1)*4;
        figure,imagesc(tt),
        title([proj_meta(site).animal ' - ' ...
            num2str(proj_meta(site).ExpGroup(1)) ' - tp: ' num2str(tp)]);
    end
%     if show_avg_act_vis_speed
%         figure(jj);
%         plot(mean(act,1)*1.5+2*(tp-1),'r')
%         plot(proj_meta(site).rd(lyr,tp).vis_speed/10+2*(tp),'b')
%     end
    for k = 1:length(type_sessions)
        ac_sess=find(strcmp(proj_meta(site).rd(lyr,tp).session,type_sessions(k))==1);
        indices=[];
        for kk=1:length(ac_sess)
            indices=[indices temp(ac_sess(kk)):temp(ac_sess(kk)+1)-1];
        end
        
        switch k
            case 1 % f
                velM_r = find(velM>0.005);
                velP_r = find(velP>0.005);
                velM_r = intersect(velM_r,velP_r);
                temp2=mean(act(:,intersect(indices,velM_r)),2);
                mean_act(tp,k)=mean(temp2);
                sem_act(tp,k)=std(temp2)/sqrt(length(temp2));
            case 2 % p
                velM_r = find(abs(velM)<0.005); % no running
                velP_r = find(velP>0.005); % but stim moving
                velM_r = intersect(velM_r,velP_r);
                temp2=mean(act(:,intersect(indices,velM_r)),2);
                mean_act(tp,k)=mean(temp2);
                sem_act(tp,k)=std(temp2)/sqrt(length(temp2));
            case 3 % d
                velM_r = find(velM>0.005);
                velM_b = find(abs(velM)<0.0005); % base lin
                temp2=mean(act(:,intersect(indices,velM_r)),2);
                mean_act(tp,k)=mean(temp2);
                sem_act(tp,k)=std(temp2)/sqrt(length(temp2));
                
                temp2=mean(act(:,intersect(indices,velM_b)),2);
                mean_act(tp,k+1)=mean(temp2);
                sem_act(tp,k+1)=std(temp2)/sqrt(length(temp2));
        end
    end
    k=5; % mean ac
    temp2=mean(act,1);
    mean_act(tp,k)=mean(temp2);
    sem_act(tp,k)=std(temp2)/sqrt(length(temp2));
end
    if show_avg_act_vis_speed
        figure(jj);hold on
        plot(mean(act,1)*1.5+3*(site-1),'r')
        plot(velM./max(velM)+3*(site-0.5),'g')
        plot(velP./max(velP)+3*(site),'b')
%         plot(proj_meta(site).rd(lyr,tp).vis_speed/10+2*(site),'b')
    end
figure(ii);
set(gcf,'DefaultAxesColorOrder',colors(1:no_time_points,:));
subplot(4,2,site);
% plot(mean_act','o-')
errorbar(mean_act',sem_act','o')
title([proj_meta(site).animal ' - ' num2str(proj_meta(site).ExpGroup(1))]);
% figure(oo);
% set(gcf,'DefaultAxesColorOrder',colors2(1:size(mean_act,1),:));
% subplot(3,2,site);plot(mean_act,'o-')
% title([proj_meta(site).animal ' - ' num2str(proj_meta(site).ExpGroup(1))]);

end


